package cn.lyj.study.service;

import cn.lyj.study.mapper.BusinessMapper;
import cn.lyj.study.pojo.Business;
import cn.lyj.study.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired(required = false)
    BusinessMapper businessMapper;
    @Override
    public PageInfo<Business> getBusiness(int currentPage, int pageSize) {
        //第一步  获取所有的商家
        List<Business> allBusiness = businessMapper.getAllBusiness();
        //获取分页对象
        PageInfo<Business> businessPageInfo = new PageInfo<>(allBusiness.size(), pageSize);
        // 进行分页的逻辑处理
        businessPageInfo.doPaging(currentPage);
        if(currentPage == businessPageInfo.getTotalPages()){
            businessPageInfo.setEnd(allBusiness.size());
        }else {
            businessPageInfo.setEnd(businessPageInfo.getStart()+businessPageInfo.getPageSize());
        }
        List<Business> businesses = allBusiness.subList(businessPageInfo.getStart(), businessPageInfo.getEnd());
        businessPageInfo.setList(businesses);
        return businessPageInfo;
    }

    @Override
    public boolean addBusiness(Business business) {
        int n = businessMapper.addBusiness(business) ;
        if(n > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateBusiness(Business business) {
        int n = businessMapper.updateBusiness(business) ;
        if(n > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delectBusiness(int businessId) {
        int n = businessMapper.delectBusiness(businessId) ;
        if(n > 0){
            return true;
        }
        return false;
    }
}
